Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

848
Views
Using PostgreSQL, how do I escape "\" in json columns?

I'm using Postgres 9.5 and I have a column ("info") of type 'json'... I'm trying to do this insert:

INSERT INTO table ( info )
VALUES (
  '{"entry":"((\+)[0-9]+)"}'
)

but I get this error:

ERROR:  invalid input syntax for type json
DETAIL:  Escape sequence "\+" is invalid.

It's interpreting \+ as an escape sequence but I actually want that as part of my value.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In general,

  • if you have keys and values use jsonb_build_object to construct the object
  • if you're writing a literal JSON object to cast from string, then it must be proper; a proper JSON string requires escaping \.

Explanation

PostgreSQL isn't quoting this: it's just the JSON implimentation following RFC 7159.

A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F). [... ] So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\".

So it looks like this in the literal form.

CREATE TABLE tbl
AS
  SELECT '{"entry":"((\\+)[0-9]+)"}'::jsonb AS info;

Dollar-quoting in PostgreSQL requires no-escapes, but it will not help here,

Notice that inside the dollar-quoted string, single quotes can be used without needing to be escaped. Indeed, no characters inside a dollar-quoted string are ever escaped: the string content is always written literally. Backslashes are not special, and neither are dollar signs, unless they are part of a sequence matching the opening tag.

So this will not work because \+ is not a valid string in JSON. It would work if we were not using a json type though.

SELECT '{"key":"\"}'::jsonb;
ERROR:  invalid input syntax for type json
LINE 1: SELECT '{"key":"\"}'::jsonb;
               ^
DETAIL:  Token ""\"}" is invalid.
CONTEXT:  JSON data, line 1: {"key":"\"}

However, you can use to_jsonb() to JSON-escape the strings..

SELECT FORMAT( $${%s:%s}$$, to_jsonb(k), to_jsonb(v) )::jsonb
FROM ( VALUES
  ('key', '\' )
) AS t(k,v);

But, even this is a bad idea because if you have the keys and values you can use json_build_object,

SELECT jsonb_build_object( k, v )
FROM ( VALUES
  ('key', '\' )
) AS t(k,v);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!